const express = require('express'); // 框架
const url = require('url'); // url模块
const moment = require('moment');

const response = require('module/response'); // 响应
const validate = require('module/validate'); // 验证
const Page = require('entity/page'); // 分页

const flashService = require('service/flash-service');

const router = express.Router(); // 路由

/**
 * 获取分页数据
 */
router.get('/getPageList/:manage?', (req, res, next) => {
  let sourceManage = req.params.manage === '1'; // 来源是否是管理系统
  let paramsT = url.parse(req.url, true).query; // url参数获取
  // 参数转换
  let params = {
    pageNum: Number(paramsT.pageNum),
    pageSize: Number(paramsT.pageSize),
    title: paramsT.title, // 文件标题
    type: paramsT.type ? Number(paramsT.type) : '', // 类型
    enable_state: paramsT.enable_state ? Number(paramsT.enable_state) : '', // 启用状态
    audit_state: paramsT.audit_state ? Number(paramsT.audit_state) : '' // 审核状态
  }
  // 如果非后台管理系统-设置参数
  if (!sourceManage) {
    params.enable_state = 1; // 启用状态
    params.audit_state = 2; // 审核状态
  }
  // 参数验证
  let msgs = validate(params, {
    pageNum: {str: '页码', type: 'number'},
    pageSize: {str: '每页条数', type: 'number'},
    title: {str: '文件标题', type: 'string'},
    type: {str: '类型', type: 'number'},
    enable_state: {str: '启用状态', type: 'number'},
    audit_state: {str: '审核状态', type: 'number'}
  });
  if (msgs) {
    response(res, null, null, 8001, msgs);
    return false;
  }
  flashService.getPageList(params, (err, result) => {
    let page = new Page(params.pageNum, params.pageSize, result ? result.total : 0, result);
    response(res, page, err);
  });
});

/**
 * 通过ID获取数据
 */
router.get('/detail/:id', (req, res, next) => {
  let id = req.params.id; // 对象参数获取
  flashService.detail(id, (err, result) => {
    if (result) {
      result.update_time = moment(result.update_time).format('YYYY-MM-DD');
      response(res, result, err);
    } else {
      response(res, null, err, 8002);
    }
  });
});

/**
 * 新增
 */
router.post('/add', (req, res, next) => {
  let paramsT = req.body; // 参数获取
  // 参数转换
  let params = {
    type: paramsT.type, // 类型
    title: paramsT.title, // 文件标题
    remark: paramsT.remark, // 备注
    img: paramsT.img, // 预览图路径
    url: paramsT.url // 文件路径
  }
  // 参数验证
  let msgs = validate(params, {
    type: {str: '类型', required: true, type: 'number'},
    title: {str: '标题', required: true, length: [1, 10]},
    remark: {str: '备注', length: [1, 50]},
    img: {str: '预览图', required: true, length: [1, 100]},
    url: {str: '文件', required: true, length: [1, 100]}
  });
  if (msgs) {
    response(res, null, null, 8001, msgs);
    return false;
  }
  flashService.add(params, (err, result) => {
    response(res, null, err);
  });
});

/**
 * 修改
 */
router.post('/update', (req, res, next) => {
  let paramsT = req.body; // 参数获取
  // 参数转换
  let params = {
    id: paramsT.id,
    type: paramsT.type, // 类型
    title: paramsT.title, // 文件标题
    remark: paramsT.remark, // 备注
    img: paramsT.img, // 预览图路径
    url: paramsT.url // 文件路径
  }
  // 参数验证
  let msgs = validate(params, {
    id: {required: true},
    type: {str: '类型', required: true, type: 'number'},
    title: {str: '标题', required: true, length: [1, 10]},
    remark: {str: '备注', length: [1, 50]},
    img: {str: '预览图', required: true, length: [1, 100]},
    url: {str: '文件', required: true, length: [1, 100]}
  });
  if (msgs) {
    response(res, null, null, 8001, msgs);
    return false;
  }
  flashService.update(params, (err, result) => {
    response(res, null, err);
  });
});

/**
 * 删除
 */
router.post('/remove', (req, res, next) => {
  let paramsT = req.body; // 参数获取
  // 参数转换
  let params = {
    ids: paramsT.ids // id列表
  }
  // 参数验证
  let msgs = validate(params, {
    ids: {str: 'id列表', required: true}
  });
  if (msgs) {
    response(res, null, null, 8001, msgs);
    return false;
  }
  flashService.remove(params.ids, (err, result) => {
    response(res, null, err);
  });
});

module.exports = router;
